define(['angular',"text!www/trends/community.html",'url','common','swiper','jqload'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$http,communService){
	   hideMenu();
	   initFooter(0);
	   showSpin();
	   $rootScope.notify = cl.user.notify;
	   $scope.community = communService;
	   $scope.community.initCommunity = function(){
	    	 if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	         this.busy = true;
	         var pro = this;
	         var url = cl.getCommunityUrl+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
		        	hideSpin();
					if(res.code=="CL02045"){//获取成功
						pro.news = res.result.news;
						pro.banners = res.result.ad;
						pro.wxid = res.result.wxid;
						communService.setCommun(pro);
					}
					
					setTimeout("initBox();jq_load();",1000);
					pro.busy = false;
	         });
	     };
	     $scope.community.likeNew = function(news_id,index){
		    	if(!this.wxid){
		    		showAlert("请登录后方可点赞");
		    		return false;
		    	}
		    	if (this.busy){
			       	 hideSpin();
			       	 return;
			        }
			    this.busy = true;
			    var pro = this;
			    var url = cl.likeNewUrl+"&wxid="+this.wxid +"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
			    $http.jsonp(url).success(function (res) {
					if(res.code=="CL02036"){//获取成功
						if(res.result.codeMsg=="cancel"){
							pro.news[index].like_num =  parseInt(pro.news[index].like_num) - 1;
							pro.news[index].is_like = "";
							communService.setNews(pro.news);
							showAlert("取消点赞");
						}else{
							pro.news[index].like_num = parseInt(pro.news[index].like_num) + 1;
							pro.news[index].is_like = "liked";
							communService.setNews(pro.news);
							showAlert("成功点赞");
						}
					}else{
						showAlert("点赞失败，请登录后再操作");
					}
					pro.busy = false;
			    });
		    };
	     $scope.community.collectNews = function(news_id,index){
	    	 if (this.busy){
	        	 return;
	         }
	    	 if(!this.wxid>0)
	         {
	    		 showAlert("登录后方可收藏");
	    		 return;
	         }
	    	 this.busy = true;
	    	 var pro = this;
	         var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							pro.top_news[index].collect_times =  parseInt(pro.top_news[index].collect_times) - 1;
							showAlert("取消收藏");
							pro.top_news[index].is_collect = " ";
						}else{
							pro.top_news[index].collect_times =  parseInt(pro.top_news[index].collect_times) + 1;
							showAlert("已加入收藏");
							pro.top_news[index].is_collect = "collected";
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
					}
					pro.busy = false;
	         });
	     };
	 	
	 	$scope.community.nextPage = function(){//上拉刷新
	 		if(!isFooter()) return ;
	 		if (this.busy) return;
		       this.busy = true;
		       var pro = this;
		       if(this.news.length>0){
		    	   site = parseInt(this.news.length)-1;
		    	   var endTime = this.news[site].create_time;
		       }else{
		    	   var endTime = "";
		       }
		       var url = cl.refreshCommunityUrl+"&callback=JSON_CALLBACK&endTime="+ endTime+"&wxid="+pro.wxid ;
		       $http.jsonp(url).success(function (res) {
		      	//处理成功的情况
		    	   if(res.code=="CL02046"){
		    		   if(res.result.length!=0){
				             var items = res.result;
				             for (var i = 0; i < items.length; i++) {
				            	 pro.news.push(items[i]);
				             }
						}
		    		   communService.setNews(pro.news);
			      	}
		    	   pro.busy = false;
		       });
	 	};
	 	$scope.community.delNews = function(id,index){//上拉刷新
	 		if (this.busy)
			{
		       	 return;
	        }
		    this.busy = true;
		    showSpin();
			var url = cl.delNewsUrl+"&callback=JSON_CALLBACK&id="+id+"&wxid="+this.wxid;
			var pro = this;
			showConfirm("确定删除？",function()
			{
				 $http.jsonp(url).success(function (res) {
						if(res.code=="CL02048"){//删除成功
							showAlert("成功删除该帖子");
			    			pro.news.splice(index,1);
			    			communService.setNews(pro.news);
						}else{
							showAlert("删除帖子失败");
						}
						pro.busy = false;
						hideSpin();
				 })
			},function(){
				pro.busy = false;
				hideSpin();
			});
	 	};
	 	
	 	if(communService.news.length<=0||communService.news.length>=100){//缓存100个数据，超过则进入该页面要重新请求
 	 		$scope.community.initCommunity();
 	 	}else{
 	 		setTimeout('initBox();jq_load();',1500);
 	 		hideSpin();
 	 	}
	 	$scope.community.seeImg = function(newindex,index){
	 		var pro = this;
	 		var items = pro.news[newindex].thumb;
	 		var urls = [];
            for (var i = 0; i < items.length; i++) {
           	  urls[i] =pro.base+items[i].img;
            }
	 		wx.previewImage({
	 		    current: pro.base+pro.news[newindex].thumb[index].img, // 当前显示图片的http链接
	 		    urls: urls // 需要预览的图片http链接列表
	 		});
	 	};
	 	$scope.community.collectNews = function(news_id,index){
			 if (this.busy){
	        	 return;
	         }
	    	 if(!this.wxid>0)
	         {
	    		 showAlert("登录后方可收藏");
	    		 return;
	         }
	    	 this.busy = true;
	    	 var pro = this;
	         var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							showAlert("取消收藏");
							pro.news[index].collect_times =  parseInt(pro.news[index].collect_times) - 1;
							pro.news[index].is_collect = " ";
						}else{
							pro.news[index].collect_times =  parseInt(pro.news[index].collect_times) + 1;
							showAlert("已加入收藏");
							pro.news[index].is_collect = "collected";
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
					}
					pro.busy = false;
	         });
		};
    };
    return {controller:controller,tpl:tpl};
});
